//
//  KSWebService.m
//  KSLib
//
//  Created by User on 9/27/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "KSWebService.h"


@implementation KSWebService

@synthesize webData;

-(id) callWebService:(NSString *) prmSoapMessage
               andServiceLocaion:(NSString *) prmWebseriveLocation
                 andContentType :(NSString *) prmContentType
                   andSoapAction: (NSString *) prmSoapAction
                       andMethod:(NSString *) prmMethod{

    
    NSURL *url = [NSURL URLWithString:prmWebseriveLocation];			  
				  	
    
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    
    
    //—-set the various headers—-				  
	NSString *msgLength = [NSString stringWithFormat:@"%d", [prmSoapMessage length]];	
    
	[req addValue:prmContentType forHTTPHeaderField:@"Content-Type"];
	//[req addValue:prmSoapAction forHTTPHeaderField:@"SOAPAction"];
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];	

    //—-set the HTTP method and body—-				  
	[req setHTTPMethod:prmMethod];				  
	[req setHTTPBody:[prmSoapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    

	conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    
	if (conn) {					  
	    webData = [[NSMutableData data] retain];					  
	}	
    
    return self;
}
-(void) connection:(NSURLConnection *) connection
didReceiveResponse:(NSURLResponse *) response {
    [webData setLength: 0];
}

-(void) connection:(NSURLConnection *) connection
    didReceiveData:(NSData *) data {
    [webData appendData:data];
}

-(void) connection:(NSURLConnection *) connection
  didFailWithError:(NSError *) error {
    [webData release];
    [connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    
    /*
    NSString *theXML = [[NSString alloc]						
                        initWithBytes: [webData mutableBytes]						
                        length:[webData length]						
                        encoding:NSUTF8StringEncoding];
     
     */
	
    //—-shows the XML—-	
    //NSLog(@"%@",theXML);	

    //[webData release];	
}

@end
